﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Assessment.Management.SharedComponent.Params.MenuManagers;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;

namespace Assessment.Management.UI.Admin
{
    public partial class UniAdmin : System.Web.UI.Page
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {


            AccountParam param = GetAccount();
            LoadMenuManager(param.Account.Permission);
            BindUserControls();
            
        }

        protected void rptParentMenuV_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hd = (HiddenField)e.Item.FindControl("hdParentIDV");
            Repeater rptChild = (Repeater)e.Item.FindControl("rptChildMenuV");
            MenuManagersParam param = new MenuManagersParam(FunctionType.GetChildMenu);
            param.MenuManagers = new MenuManager() { MenuParentID = int.Parse(hd.Value) };
            MainController.Provider.Execute(param);
            rptChild.DataSource = param.listMenuManagers;
            rptChild.DataBind();
        }

        #endregion

        #region methods

        private void BindUserControls()
        {
            string id = Request.QueryString["id"];

            string menudo = Request.QueryString["do"];
            //string menudo = "polldisplay";

            // Hiển thị Menu con
            HtmlGenericControl gen = new HtmlGenericControl();
            Control control = null;

            switch (menudo)
            {
                case "statisticpoll": control = Page.LoadControl("UserControls/Statistic/StatisticPoll.ascx"); break;
                case "teachermanager": control = Page.LoadControl("UserControls/Teachers/TeacherMannager.ascx"); break;
                case "teacherdisplay": control = Page.LoadControl("UserControls/Teachers/TeacherDisplay.ascx"); break;
                case "teacheredit": control = Page.LoadControl("UserControls/Teachers/TeacherEdit.ascx"); break;
                case "teacheradd": control = Page.LoadControl("UserControls/Teachers/TeacherAdd.ascx"); break;
                case "classmanager": control = Page.LoadControl("UserControls/Classes/ClassManager.ascx"); break;
                case "classadd": control = Page.LoadControl("UserControls/Classes/ClassAdd.ascx"); break;
                case "classedit": control = Page.LoadControl("UserControls/Classes/ClassEdit.ascx"); break;
                case "classdisplay": control = Page.LoadControl("UserControls/Classes/ClassDisplay.ascx"); break;
                case "pollsetting": control = Page.LoadControl("UserControls/Statistic/PollSetting.ascx"); break;
                case "polladd": control = Page.LoadControl("UserControls/Statistic/PollAdd.ascx"); break;
                case "polldisplay": control = Page.LoadControl("UserControls/Statistic/PollDisplay.ascx"); break;
                case "polledit": control = Page.LoadControl("UserControls/Statistic/PollEdit.ascx"); break;
                case "permission": control = Page.LoadControl("UserControls/Permissions/PermissionManager.ascx"); break;
                case "permissionadd": control = Page.LoadControl("UserControls/Permissions/PermissionAdd.ascx"); break;
                case "viewprofile": control = Page.LoadControl("UserControls/ViewProfile.ascx"); break;
                case "subjectadd": control = Page.LoadControl("UserControls/Subjects/SubjectAdd.ascx"); break;
                case "subjectedit": control = Page.LoadControl("UserControls/Subjects/SubjectEdit.ascx"); break;
                case "subjectdisplay": control = Page.LoadControl("UserControls/Subjects/SubjectDisplay.ascx"); break;
                case "subjectmanager": control = Page.LoadControl("UserControls/Subjects/SubjectManager.ascx"); break;
                case "statisticbyteacher": control = Page.LoadControl("UserControls/Statistic/StatisticByTeacher.ascx"); break;
                case "statisticbyteacherdetail": control = Page.LoadControl("UserControls/Statistic/StatisticByTeacherDetail.ascx"); break;
                //case "config": control = Page.LoadControl("UserControls/Config.ascx"); break;
                default:
                    break;
            }
            if (control != null)
            {
                gen.Controls.Add(control);
                Body.Controls.Add(gen);
            }
        }

        private void LoadMenuManager(int permission)
        {
            MenuManagersParam param = new MenuManagersParam(FunctionType.GetMenuManagers);
            param.MenuManagers = new MenuManager() { Permission = permission };
            MainController.Provider.Execute(param);
            rptParentMenuV.DataSource = param.listMenuManagers;
            rptParentMenuV.DataBind();
        }

        private AccountParam GetAccount()
        {
            //TODO: Edit account
            //Nếu trực tiếp vào trang login
            //if (Request.Cookies["username"] == null)
            //{
            //    Response.Redirect("Login.aspx");
            //    return;
            //}
            //string user = Request.Cookies["username"].Value;
            //Account acc = db.Accounts.SingleOrDefault(a => a.Username.ToString() == user);
            //if (acc == null)
            //{
            //    Response.Redirect("Login.aspx");
            //    return;
            //}
            //lblUsername.Text = acc.Username;

            AccountParam param = new AccountParam(FunctionType.GetAccount);
            param.Account = new Account() { Username = "bangtv91" };
            MainController.Provider.Execute(param);
            if (param.Account == null)
            {
                Response.Redirect("Login.aspx");
            }
            lblUsername.Text = param.Account.Username;
            return param;
        }
        #endregion
    }
}